package pack;


import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class UI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	
	public ArrayList<JTextPane> reg_special_txt;	    //RDados,Rend,IR,Mux,L/E
	public ArrayList<JRadioButton> reg_special_rad;	//RDados,Rend,IR,Mux,L/E
	public ArrayList<JTextPane> reg_common_txt;	//RA,R0,R1,PC,R2,R3,R4,RB
	public ArrayList<JRadioButton> reg_common_rad; //RA,R0,R1,PC,R2,R3,R4,RB
	public ArrayList<JRadioButton> ula_rad;
	public ArrayList<JRadioButton> muxa_rad;
	public ArrayList<JRadioButton> muxb_rad;
	public ArrayList<JRadioButton> menu_options_item;
	public ArrayList<JCheckBox> menu_flags_item;
	public JMenuBar menu_bar;
	public JMenu menu_file;
	public JMenu menu_options;
	public JMenuItem mntmRun;	//MenuItem Run ctrl+R
	public JMenuItem mntmLoad;	//MenuItem Load ctrl+L
	public JScrollPane panel;
	public JTable table;	//Tabela da memoria
	public DefaultTableModel model;	//Usa pra editar os valores da memoria, col=1 sempre
	public JPanel panel_ula;	//Pra mostar o codigo da ULA
	public JButton btnLoad;
	public JButton btnExecutar;
	private JLabel lblMouseOver;
	public CPU cpu;
	private JEditorPane panel_log;
	private JScrollPane panel_log_scroll;
	public JLabel label_mux;
	public JLabel label_muxa;
	public JLabel label_muxb; 
	
	//Return Execution mode, 0-Normal , 1-Inst, 2-Micro
	public int getMode(){
		int mod = 0;
		for(int i = 0;i < menu_options_item.size();i++){
			if(menu_options_item.get(i).isSelected()){
				mod = i;
			}
		}
		return mod;
	}
	
	public void executar(){
		//CADA MICRO
		if (getMode()==2){
            if(cpu.ir != 0x1200){
                cpu.execute();
            }
            else btnExecutar.setEnabled(false);
        }
		//CADA INSTRU��O
        else if (getMode()==1){
            if (cpu.ir != 0x1200){
            	do{
                    cpu.execute();
                }while( (!(cpu.execMicro.isEmpty()) && cpu.ir != 0x1200 ));
            }
            else btnExecutar.setEnabled(false);
        }
        else//TODO PROGRAMA
        {
            while( cpu.ir != 0x1200 )
            {
                cpu.execute();
            }
            btnExecutar.setEnabled(false);
        }
	}
	
	public String loadFile(){
		JFileChooser chooser = new JFileChooser();    
		chooser.setApproveButtonText("Select");
		chooser.setCurrentDirectory(new File("./testes"));
		String str = new String();
		if (chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
		    File dir = chooser.getCurrentDirectory();
			String file = chooser.getSelectedFile().getName();
			String directory = dir.getPath();
			directory = directory.replace('\\', '/');
		
			str = directory+ '/' + file;
		}
		return str;
	}
	
	public void updateUI(){
		System.out.println("UI: Atualizando Interface");
		
		//Atualizando Log
		//panel_log.removeAll();
		panel_log.setText("");
		String str = new String();
		for(int i = 0;i < cpu.logMicro.size();i++){
			str +=cpu.logMicro.get(i);
			str += '\n';
		}
		panel_log.setText(str);
		
		//Update Flags
		for(int i = 0;i < menu_flags_item.size();i++){
			menu_flags_item.get(i).setSelected(cpu.flags[i]);
		}
		
		//Update Memory
		for(int i = 0;i < cpu.mPrincipal.memory.size();i++){
			String val = "0x";
			val += String.format("%04X", cpu.mPrincipal.memory.get(i));
			model.setValueAt(val, i, 1);
		}
		
		//Update //RA,R0,R1,PC,R2,R3,R4,RB
		for(int i = 0;i < reg_common_rad.size();i++){
			reg_common_rad.get(i).setSelected(cpu.sinais[i]==1);
		}
		reg_common_txt.get(0).setText("0x" + String.format("%04X", cpu.ra));
		reg_common_txt.get(1).setText("0x" + String.format("%04X", cpu.r0));
		reg_common_txt.get(2).setText("0x" + String.format("%04X", cpu.r1));
		reg_common_txt.get(3).setText("0x" + String.format("%04X", cpu.pc));
		reg_common_txt.get(4).setText("0x" + String.format("%04X", cpu.r2));
		reg_common_txt.get(5).setText("0x" + String.format("%04X", cpu.r3));
		reg_common_txt.get(6).setText("0x" + String.format("%04X", cpu.r4));
		reg_common_txt.get(7).setText("0x" + String.format("%04X", cpu.rb));
		
		//Update //RDados,Rend,IR,Mux,L/E
		reg_special_rad.get(0).setSelected(cpu.sinais[16]==1); reg_special_txt.get(0).setText("0x" + String.format("%04X", cpu.rdados));
		reg_special_rad.get(1).setSelected(cpu.sinais[14]==1); reg_special_txt.get(1).setText("0x" + String.format("%04X", cpu.rend));
		reg_special_rad.get(2).setSelected(cpu.sinais[13]==1); reg_special_txt.get(2).setText("0x" + String.format("%04X", cpu.ir));
		reg_special_rad.get(3).setSelected(cpu.sinais[17]==1); 
		reg_special_rad.get(4).setSelected(cpu.sinais[15]==1); 
		
		//Update Ula signs
		for(int i = 0;i < ula_rad.size();i++){
			ula_rad.get(i).setSelected(cpu.sinais[i+18]==1);
		}
		//Update Mux A/B signs
		for(int i = 0;i < muxa_rad.size();i++){
			muxa_rad.get(i).setSelected(cpu.sinais[i+8]==1);
		}
		for(int i = 0;i < muxb_rad.size();i++){
			muxb_rad.get(i).setSelected(cpu.sinais[i+11]==1);
		}
		
		//Atualizando Codigos Mux's A/B/Mux
		if(reg_special_rad.get(3).isSelected()){	//Mux
			label_mux.setText("________X");
		}
		else{
			label_mux.setText("X________");
		}
		//label_muxa = new JLabel("X__X__X___X__X");
		boolean f1 = muxa_rad.get(2).isSelected();
		boolean f2 = muxa_rad.get(1).isSelected();
		boolean f3 = muxa_rad.get(0).isSelected();
		if(!f3 && !f2 && !f1){
			label_muxa.setText("X_____________");
		}
		else if(!f3 && !f2 && f1){
			label_muxa.setText("___X__________");
		}
		else if(!f3 && f2 && !f1){
			label_muxa.setText("______X_______");
		}
		else if(!f3 && f2 && f1){
			label_muxa.setText("__________X___");
		}
		else if(f3 && !f2 && !f1){
			label_muxa.setText("_____________X");
		}
		else{
			label_muxa.setText("______________");
		}
		//label_muxb = new JLabel("X__X___X__X");
		f1 = muxb_rad.get(1).isSelected();
		f2 = muxb_rad.get(0).isSelected();
		if(!f2 && !f1){
			label_muxb.setText("X__________");
		}
		else if(!f2 && f1){
			label_muxb.setText("___X_______");
		}
		else if(f2 && !f1){
			label_muxb.setText("_______X___");
		}
		else {
			label_muxb.setText("__________X");
		}
	}

	/**
	 * Create the frame.
	 */
	@SuppressWarnings("serial")
	public UI() {
		super("Trabalho Arquitetura");
		try {  
		    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		            
	     } catch (IllegalAccessException e) {} 
		    catch (ClassNotFoundException e) {} 
		    catch (InstantiationException e) {} 
		    catch (UnsupportedLookAndFeelException e) { }
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(30, 20, 815, 660);
		this.setMinimumSize(new Dimension(815,660));
		
		//Other Class
		cpu = new CPU();
		
		menu_bar = new JMenuBar();
		setJMenuBar(menu_bar);
		
		menu_file = new JMenu("File");
		menu_bar.add(menu_file);
		
		mntmRun = new JMenuItem("Run");
		mntmRun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("UI: Apertei Rodar!! Mode: " + getMode());
				executar();
				updateUI();
			}
		});
		mntmRun.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
		menu_file.add(mntmRun);
		
		menu_options = new JMenu("Options");
		menu_bar.add(menu_options);
		
		menu_options_item = new ArrayList<JRadioButton>();
		menu_options_item.add(new JRadioButton("Normal"));
		menu_options_item.get(0).setSelected(true);
		menu_options_item.add(new JRadioButton("Instr x Instr"));
		menu_options_item.add(new JRadioButton("Micro x Micro"));
		menu_options_item.get(0).addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		menu_options_item.get(0).setSelected(true);
        		menu_options_item.get(1).setSelected(false); 
        		menu_options_item.get(2).setSelected(false);
            }
        });
		menu_options_item.get(1).addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		menu_options_item.get(0).setSelected(false);
        		menu_options_item.get(1).setSelected(true); 
        		menu_options_item.get(2).setSelected(false);
            }
        });
		menu_options_item.get(2).addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		menu_options_item.get(0).setSelected(false);
        		menu_options_item.get(1).setSelected(false); 
        		menu_options_item.get(2).setSelected(true);
            }
        });
		menu_options.add(menu_options_item.get(0));
		menu_options.add(menu_options_item.get(1));
		menu_options.add(menu_options_item.get(2));
		
		JLabel flag_label = new JLabel("Flags:");
		flag_label.setBounds(660, 170, 89, 15);
		
		JPanel panel_flags = new JPanel();
		panel_flags.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_flags.setBackground(Color.WHITE);
		panel_flags.setBounds(660, 185, 105, 113);
		panel_flags.setLayout(new GridLayout(0, 1, 0, 0));
		
		menu_flags_item = new ArrayList<JCheckBox>();
		menu_flags_item.add(new JCheckBox("Equal")); 
		menu_flags_item.add(new JCheckBox("Greater"));
		menu_flags_item.add(new JCheckBox("Lower"));
		menu_flags_item.add(new JCheckBox("Zero"));
		menu_flags_item.add(new JCheckBox("Negative"));
		menu_flags_item.add(new JCheckBox("Carry"));
		for(int i = 0;i < 6;i++){
			final int ix = i;
			menu_flags_item.get(i).setBackground(new Color(255,255,255));
			menu_flags_item.get(i).addActionListener(new ActionListener() {
	        	public void actionPerformed(ActionEvent arg0) {
	        		boolean b = menu_flags_item.get(ix).isSelected();
	        		menu_flags_item.get(ix).setSelected(!b);
	            }
	        });
			panel_flags.add(menu_flags_item.get(i));
		}
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(panel_flags);
		
		//JLabel label = new JLabel("V_______V");
		label_mux = new JLabel("X________");
		label_mux.setBounds(52, 45, 70, 14);
		label_mux.setFont(new Font("TimesNewRoman", Font.PLAIN, 11));
		
		//label_muxa = new JLabel("X__X__X___X__X");
		label_muxa = new JLabel("X_____________");
		label_muxa.setBounds(302, 194, 123, 14);
		label_muxa.setFont(new Font("TimesNewRoman", Font.PLAIN, 11));
		
		//label_muxb = new JLabel("X__X___X__X");
		label_muxb = new JLabel("X__________");
		label_muxb.setBounds(510, 184, 123, 14);
		label_muxb.setFont(new Font("TimesNewRoman", Font.PLAIN, 11));
		
		contentPane.add(label_mux);
		contentPane.add(label_muxa);
		contentPane.add(label_muxb);
		contentPane.add(flag_label);
		
		//Registers Text/Enable init
		this.reg_common_rad = new ArrayList<JRadioButton>();
		this.reg_common_txt = new ArrayList<JTextPane>();
		for(int i = 0;i < 8;i++){
			reg_common_rad.add(new JRadioButton(""));
			reg_common_rad.get(reg_common_rad.size()-1).setBackground(Color.WHITE);
			reg_common_rad.get(reg_common_rad.size()-1).setBounds(195+(i*74+i*2), 60, 23, 23);
			final int ix = i;
			reg_common_rad.get(reg_common_rad.size()-1).addActionListener(new ActionListener() {
	        	public void actionPerformed(ActionEvent arg0) {
	        		boolean b = reg_common_rad.get(ix).isSelected(); 
	        		reg_common_rad.get(ix).setSelected(!b);
	            }
	        });
			contentPane.add(reg_common_rad.get(reg_common_rad.size()-1));
			
			reg_common_txt.add(new JTextPane());
			reg_common_txt.get(reg_common_txt.size()-1).setFont(new Font("Tahoma", Font.PLAIN, 11));
			reg_common_txt.get(reg_common_txt.size()-1).setText("0xBEEF");
			reg_common_txt.get(reg_common_txt.size()-1).setEditable(false);
			reg_common_txt.get(reg_common_txt.size()-1).setBorder(new LineBorder(new Color(0, 0, 0)));
			reg_common_txt.get(reg_common_txt.size()-1).setBounds(150+(i*76), 88, 45, 20);
			contentPane.add(reg_common_txt.get(reg_common_txt.size()-1));
		}
		
		//Registers specials REND,IR,etc textPanes
		reg_special_txt = new ArrayList<JTextPane>();
		
		reg_special_txt.add(new JTextPane());	//--------------RDados-------------
		reg_special_txt.get(reg_special_txt.size()-1).setFont(new Font("Tahoma", Font.PLAIN, 12));
		reg_special_txt.get(reg_special_txt.size()-1).setText("0xFEED");
		reg_special_txt.get(reg_special_txt.size()-1).setBorder(new LineBorder(new Color(0, 0, 0)));
		reg_special_txt.get(reg_special_txt.size()-1).setEditable(false);;
		reg_special_txt.get(reg_special_txt.size()-1).setBounds(52, 160, 50, 20);
		contentPane.add(reg_special_txt.get(reg_special_txt.size()-1));
		
		reg_special_txt.add(new JTextPane());	//--------------REnd-------------
		reg_special_txt.get(reg_special_txt.size()-1).setFont(new Font("Tahoma", Font.PLAIN, 12));
		reg_special_txt.get(reg_special_txt.size()-1).setText("0xFEED");
		reg_special_txt.get(reg_special_txt.size()-1).setBorder(new LineBorder(new Color(0, 0, 0)));
		reg_special_txt.get(reg_special_txt.size()-1).setEditable(false);
		reg_special_txt.get(reg_special_txt.size()-1).setBounds(57, 535, 50, 20);
		contentPane.add(reg_special_txt.get(reg_special_txt.size()-1));
		
		reg_special_txt.add(new JTextPane());	//--------------IR-------------
		reg_special_txt.get(reg_special_txt.size()-1).setFont(new Font("Tahoma", Font.PLAIN, 12));
		reg_special_txt.get(reg_special_txt.size()-1).setText("0xFEED");
		reg_special_txt.get(reg_special_txt.size()-1).setBorder(new LineBorder(new Color(0, 0, 0)));
		reg_special_txt.get(reg_special_txt.size()-1).setEditable(false);
		reg_special_txt.get(reg_special_txt.size()-1).setBounds(212, 412, 50, 20);
		contentPane.add(reg_special_txt.get(reg_special_txt.size()-1));
		
		//Registers specials REND,IR,etc RadioButtons
		reg_special_rad = new ArrayList<JRadioButton>();
		
		reg_special_rad.add(new JRadioButton(""));	//--------------RDados-------------
		reg_special_rad.get(reg_special_rad.size()-1).setBackground(Color.WHITE);
		reg_special_rad.get(reg_special_rad.size()-1).setBounds(115, 160, 23, 23);
		reg_special_rad.get(reg_special_rad.size()-1).setSelected(false);
		reg_special_rad.get(reg_special_rad.size()-1).addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		boolean b = reg_special_rad.get(0).isSelected(); 
        		reg_special_rad.get(0).setSelected(!b);
            }
        });
		contentPane.add(reg_special_rad.get(reg_special_rad.size()-1));
		
		reg_special_rad.add(new JRadioButton(""));	//--------------REnd-------------
		reg_special_rad.get(reg_special_rad.size()-1).setBackground(Color.WHITE);
		reg_special_rad.get(reg_special_rad.size()-1).setBounds(125, 535, 23, 23);
		reg_special_rad.get(reg_special_rad.size()-1).addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		boolean b = reg_special_rad.get(1).isSelected(); 
        		reg_special_rad.get(1).setSelected(!b);
            }
        });
		contentPane.add(reg_special_rad.get(reg_special_rad.size()-1));
		
		reg_special_rad.add(new JRadioButton(""));	//--------------IR-------------
		reg_special_rad.get(reg_special_rad.size()-1).setBackground(Color.WHITE);
		reg_special_rad.get(reg_special_rad.size()-1).setBounds(180,410, 23, 23);
		reg_special_rad.get(reg_special_rad.size()-1).setSelected(false);
		reg_special_rad.get(reg_special_rad.size()-1).addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		boolean b = reg_special_rad.get(2).isSelected(); 
        		reg_special_rad.get(2).setSelected(!b);
            }
        });
		contentPane.add(reg_special_rad.get(reg_special_rad.size()-1));
		
		reg_special_rad.add(new JRadioButton(""));	//--------------MUX-------------
		reg_special_rad.get(reg_special_rad.size()-1).setBackground(Color.WHITE);
		reg_special_rad.get(reg_special_rad.size()-1).setBounds(132,61, 23, 23);
		reg_special_rad.get(reg_special_rad.size()-1).setSelected(false);
		reg_special_rad.get(reg_special_rad.size()-1).addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		boolean b = reg_special_rad.get(3).isSelected(); 
        		reg_special_rad.get(3).setSelected(!b);
            }
        });
		contentPane.add(reg_special_rad.get(reg_special_rad.size()-1));
		
		reg_special_rad.add(new JRadioButton("L/E"));	//--------------L/E-------------
		reg_special_rad.get(reg_special_rad.size()-1).setBackground(Color.WHITE);
		reg_special_rad.get(reg_special_rad.size()-1).setBounds(170,270, 53, 23);
		reg_special_rad.get(reg_special_rad.size()-1).setFont(new Font("Tahoma", Font.PLAIN, 14));;
		reg_special_rad.get(reg_special_rad.size()-1).setHorizontalTextPosition(JRadioButton.LEFT);
		reg_special_rad.get(reg_special_rad.size()-1).setSelected(false);
		reg_special_rad.get(reg_special_rad.size()-1).addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		boolean b = reg_special_rad.get(4).isSelected(); 
        		reg_special_rad.get(4).setSelected(!b);
            }
        });
		contentPane.add(reg_special_rad.get(reg_special_rad.size()-1));
		
		//ULA settings
		ula_rad = new ArrayList<JRadioButton>();
		for(int i  = 0;i < 5;i++){
			ula_rad.add(new JRadioButton(""));
			ula_rad.get(ula_rad.size()-1).setBackground(Color.WHITE);
			ula_rad.get(ula_rad.size()-1).setBounds(290,286 +(i*27), 23, 23);
			final int ix = i;
			ula_rad.get(ula_rad.size()-1).addActionListener(new ActionListener() {
	        	public void actionPerformed(ActionEvent arg0) {
	        		boolean b = ula_rad.get(ix).isSelected(); 
	        		ula_rad.get(ix).setSelected(!b);
	            }
	        });
			contentPane.add(ula_rad.get(ula_rad.size()-1));
		}
		
		//Mux A/B settings
		muxa_rad = new ArrayList<JRadioButton>();
		muxb_rad = new ArrayList<JRadioButton>();
		for(int i  = 0;i < 3;i++){
			muxa_rad.add(new JRadioButton(""));
			muxa_rad.get(muxa_rad.size()-1).setBackground(Color.WHITE);
			muxa_rad.get(muxa_rad.size()-1).setBounds(410,186 +(i*18), 23, 23);
			final int ix = i;
			muxa_rad.get(muxa_rad.size()-1).addActionListener(new ActionListener() {
	        	public void actionPerformed(ActionEvent arg0) {
	        		boolean b = muxa_rad.get(ix).isSelected(); 
	        		muxa_rad.get(ix).setSelected(!b);
	            }
	        });
			contentPane.add(muxa_rad.get(muxa_rad.size()-1));
		}
		for(int i  = 0;i < 2;i++){
			muxb_rad.add(new JRadioButton(""));
			muxb_rad.get(muxb_rad.size()-1).setBackground(Color.WHITE);
			muxb_rad.get(muxb_rad.size()-1).setBounds(595,188 +(i*18), 23, 23);
			muxb_rad.get(muxb_rad.size()-1).setEnabled(true);
			final int ix = i;
			muxb_rad.get(muxb_rad.size()-1).addActionListener(new ActionListener() {
	        	public void actionPerformed(ActionEvent arg0) {
	        		boolean b = muxb_rad.get(ix).isSelected(); 
	        		muxb_rad.get(ix).setSelected(!b);
	            }
	        });
			contentPane.add(muxb_rad.get(muxb_rad.size()-1));
		}

		//Label with image
		JLabel lblN = new JLabel("N");
		lblN.setVerticalAlignment(SwingConstants.TOP);
		lblN.setHorizontalAlignment(SwingConstants.LEFT);
		lblN.setIcon(new ImageIcon("./images/arquitetura.GIF"));
		lblN.setBounds(0, 0, 800, 600);
		lblN.setVisible(true);
		
		//Memory stuff UI
		model = new DefaultTableModel();
		model.addColumn("Address");
		model.addColumn("Value");
		for(int lin = 0;lin < 65536;lin++){
			String[] str = new String[2];
			str[0] = "0x" + String.format("%04X",lin);
			str[1] = "0x" + String.format("%04X",0xFFFF-lin);
			model.addRow(str);
		}
		table = new JTable(model);
		table.setBackground(Color.WHITE);
		table.setCellSelectionEnabled(true);
		table.setEnabled(false);
		TableColumn Coluna = new TableColumn();
		Coluna  = table.getColumn("Address");
		
		Coluna.setCellRenderer(
	            new DefaultTableCellRenderer(){
		        @Override
		        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		            DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);      
		            renderer.setBackground(new Color(65,150,150));
					renderer.setForeground(Color.black);
					renderer.setForeground(new Color(255,255,255));;
		            setHorizontalAlignment(JLabel.CENTER);
		            return renderer;
		        }
	    });
		
		btnExecutar = new JButton("Execute");
		btnExecutar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mntmRun.doClick();
			}
		});
		btnExecutar.setBounds(465, 556, 102, 23);
		btnExecutar.setEnabled(false);
		contentPane.add(btnExecutar);
		
		//Ula ToolTip com mouse
		panel_ula = new JPanel();
		panel_ula.setBackground(new Color(235,235,140));
		panel_ula.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		panel_ula.setBounds(375, 355, 123, 175);
		panel_ula.setVisible(false);
		
		contentPane.add(panel_ula);
		panel_ula.setLayout(new GridLayout(0, 1, 0, 0));
		
		panel_ula.add(new JLabel(" Codigos Ula:"));
		panel_ula.add(new JLabel(" 00001 - Passa A"));
		panel_ula.add(new JLabel(" 00010 - Passa B"));
		panel_ula.add(new JLabel(" 00011 - +1"));
		panel_ula.add(new JLabel(" 00100 - ADD"));
		panel_ula.add(new JLabel(" 00101 - SUB"));
		panel_ula.add(new JLabel(" 00110 - CMP"));
		panel_ula.add(new JLabel(" 00111 - MOV"));
		panel_ula.add(new JLabel(" 01000 - SHL"));
		panel_ula.add(new JLabel(" 01001 - SHR"));
		panel_ula.add(new JLabel(" 01010 - NEG"));
		panel_ula.add(new JLabel(" 01011 - CLR"));
		panel_ula.add(new JLabel(" 01100 - OR"));
		panel_ula.add(new JLabel(" 01101 - AND"));
		panel_ula.add(new JLabel(" 01101 - NOT"));
		lblMouseOver = new JLabel("Mouse over Here");
		lblMouseOver.setHorizontalAlignment(SwingConstants.CENTER);
		lblMouseOver.setVerticalAlignment(SwingConstants.TOP);
		lblMouseOver.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				panel_ula.setVisible(true);
			}
			@Override
			public void mouseExited(MouseEvent e) {
				panel_ula.setVisible(false);
			}
		});
		
		JLabel label_log = new JLabel("Micro Log:");
		label_log.setBounds(633, 320, 144, 15);
		
		panel_log = new JEditorPane();
		panel_log.setLayout(new GridLayout(0, 1, 0, 0));
		panel_log.setEditable(false);
		panel_log_scroll = new JScrollPane(panel_log);
		panel_log_scroll.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_log_scroll.setBackground(Color.WHITE);
		panel_log_scroll.setBounds(633, 335, 144, 182);
		panel_log_scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		panel_log_scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		contentPane.add(panel_log_scroll);
		contentPane.add(label_log);
		lblMouseOver.setBounds(361, 325, 150, 81);
		contentPane.add(lblMouseOver);
		
		mntmLoad = new JMenuItem("Load");
		mntmLoad.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK));
		mntmLoad.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String file = loadFile();
				System.out.println("UI: Apertei Load ->" + file);
				cpu.limpar();
				cpu.leprograma(file);
				btnExecutar.setEnabled(true);
				updateUI();
			}
		});
		menu_file.add(mntmLoad);
		
		final JFrame f = this;
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("UI: Apertei Fechar!!");
				f.dispose();
			}
		});
		menu_file.add(mntmExit);
		
		btnLoad = new JButton("Load");
		btnLoad.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mntmLoad.doClick();
			}
		});
		btnLoad.setBounds(577, 556, 89, 23);
		contentPane.add(btnLoad);
		
		panel = new JScrollPane(table);
		panel.setBackground(Color.WHITE);
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(18, 250, 150, 251);
		contentPane.add(panel);
		
		contentPane.add(lblN);
	}
}
